package com.qianjing.project.clothing.controller;

import com.qianjing.common.utils.file.FileUploadUtils;
import com.qianjing.common.utils.file.MimeTypeUtils;
import com.qianjing.common.utils.poi.ExcelUtil;
import com.qianjing.framework.aspectj.lang.annotation.Log;
import com.qianjing.framework.aspectj.lang.enums.BusinessType;
import com.qianjing.framework.web.controller.BaseController;
import com.qianjing.framework.web.domain.AjaxResult;
import com.qianjing.framework.web.page.TableDataInfo;
import com.qianjing.project.clothing.domain.ClothingOverview;
import com.qianjing.project.clothing.service.IClothingOverviewService;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 服装概况Controller
 *
 * @Created by 张占宇 2023-06-12 下午3:55:54
 * @Version: V1.0.0
 */
@Validated
@RestController
@RequestMapping("/clothing/overview")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ClothingOverviewController extends BaseController {

    private final IClothingOverviewService clothingOverviewService;

    /**
     * 查询服装概况列表
     */
//    @PreAuthorize("@ss.hasPermi('clothing:overview:list')")
    @GetMapping("/list")
    public TableDataInfo list(ClothingOverview clothingOverview) {
        startPage();
        List<ClothingOverview> list = clothingOverviewService.selectClothingOverviewList(clothingOverview);
        return getDataTable(list);
    }

    /**
     * 导出服装概况列表
     */
    @PreAuthorize("@ss.hasPermi('clothing:overview:export')")
    @Log(title = "服装概况", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ClothingOverview clothingOverview) {
        List<ClothingOverview> list = clothingOverviewService.selectClothingOverviewList(clothingOverview);
        ExcelUtil<ClothingOverview> util = new ExcelUtil<ClothingOverview>(ClothingOverview.class);
        return util.exportExcel(list, "服装概况数据");
    }

    /**
     * 导入服装概况列表
     */
    @Log(title = "服装概况", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('clothing:overview:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception {
        FileUploadUtils.assertAllowed(file, MimeTypeUtils.EXCEL_EXTENSION);
        ExcelUtil<ClothingOverview> util = new ExcelUtil<ClothingOverview>(ClothingOverview.class);
        List<ClothingOverview> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = clothingOverviewService.importClothingOverview(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载服装概况导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) throws IOException {
        ExcelUtil<ClothingOverview> util = new ExcelUtil<ClothingOverview>(ClothingOverview.class);
        util.importTemplateExcel(response, "服装概况数据");
    }

    /**
     * 获取服装概况详细信息
     */
    @PreAuthorize("@ss.hasPermi('clothing:overview:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(clothingOverviewService.selectClothingOverviewById(id));
    }

    /**
     * 新增服装概况
     */
    @PreAuthorize("@ss.hasPermi('clothing:overview:add')")
    @Log(title = "服装概况", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ClothingOverview clothingOverview) {
        return toAjax(clothingOverviewService.insertClothingOverview(clothingOverview));
    }

    /**
     * 修改服装概况
     */
    @PreAuthorize("@ss.hasPermi('clothing:overview:edit')")
    @Log(title = "服装概况", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ClothingOverview clothingOverview) {
        return toAjax(clothingOverviewService.updateClothingOverview(clothingOverview));
    }

    /**
     * 删除服装概况
     */
    @PreAuthorize("@ss.hasPermi('clothing:overview:remove')")
    @Log(title = "服装概况", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(clothingOverviewService.deleteClothingOverviewByIds(ids));
    }
}
